package controller;

import entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/model")
public class ModelAttributeController {

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/f1")
    public String f1(User user){
        System.out.println("ModelAttributeController.f1()");
        user.setUsername("aaa");
        return "modelAttribute";
    }

    @RequestMapping("/f2")
    public String f2(@ModelAttribute User user){
        System.out.println("ModelAttributeController.f2()");
        user.setUsername("bbb");
        return "modelAttribute";
    }

    @RequestMapping("/f3")
    public String f3(@ModelAttribute("u") User user){
        System.out.println("ModelAttributeController.f3()");
        user.setUsername("ccc");
        return "modelAttribute";
    }

    @RequestMapping("/f4")
    public String f4(@ModelAttribute("username") String username){
        System.out.println("ModelAttributeController.f4()");
        System.out.println("username = " + username);
        username="abc";
        return "modelAttribute";
    }

    @ModelAttribute("list")
    public List<String> f4(){
        System.out.println("ModelAttributeController.f4()");
        List<String> list=new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        return list;
    }

    @RequestMapping("/f5")
    public String f5(){
        System.out.println("ModelAttributeController.f5()");
        System.out.println("request = " + request);

        User user=new User();
        user.setUsername("ddd");

        request.setAttribute("user",user);

        return "modelAttribute";
    }
}
